home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / gnu / gcctest / tests05.zoo / tstring.c < prev    next >
C/C++ Source or Header  |  1992-03-28  |  21KB  |  659 lines

  1. /*
  2.  * Test program for string(3) routines.
  3.  * 
  4.  * Note that at least one Bell Labs implementation of the string
  5.  * routines flunks a couple of these tests -- the ones which test
  6.  * behavior on "negative" characters.
  7.  */
  8.  
  9. #include <stdio.h>
  10. #include <string.h>
  11.  
  12. #define    STREQ(a, b)    (strcmp((a), (b)) == 0)
  13.  
  14. char *it = "<UNSET>";        /* Routine name for message routines. */
  15. int waserror = 0;        /* For exit status. */
  16.  
  17. char uctest[] = "\004\203";    /* For testing signedness of chars. */
  18. int charsigned;            /* Result. */
  19.  
  20. /*
  21.  - check - complain if condition is not true
  22.  */
  23. void
  24. check(thing, number)
  25. int thing;
  26. int number;            /* Test number for error message. */
  27. {
  28.     if (!thing) {
  29.         printf("%s flunked test %d\n", it, number);
  30.         waserror = 1;
  31.     }
  32. /*    else
  33.         printf("%s passed test %d\n", it, number); */
  34. }
  35.  
  36. /*
  37.  - equal - complain if first two args don't strcmp as equal
  38.  */
  39. void
  40. equal(a, b, number)
  41. char *a;
  42. char *b;
  43. int number;            /* Test number for error message. */
  44. {
  45.     check(a != NULL && b != NULL && STREQ(a, b), number);
  46. }
  47.  
  48. char one[50];
  49. char two[50];
  50.  
  51. #ifdef UNIXERR
  52. #define ERR 1
  53. #endif
  54. #ifdef BERKERR
  55. #define ERR 1
  56. #endif
  57. #ifdef ERR
  58. int f;
  59. extern char *sys_errlist[];
  60. extern int sys_nerr;
  61. extern int errno;
  62. #endif
  63.  
  64. /* ARGSUSED */
  65. main(argc, argv)
  66. int argc;
  67. char *argv[];
  68. {
  69.     /*
  70.      * First, establish whether chars are signed.
  71.      */
  72.     if (uctest[0] < uctest[1])
  73.         charsigned = 0;
  74.     else
  75.         charsigned = 1;
  76.  
  77.     /*
  78.      * Then, do the rest of the work.  Split into two functions because
  79.      * some compilers get unhappy about a single immense function.
  80.      */
  81.     first();
  82.     second();
  83.  
  84.     printf("%s\n", waserror ? "Some Errors" : "No Errors");
  85.     exit((waserror) ? 1 : 0);
  86. }
  87.  
  88. first()
  89. {
  90.     /*
  91.      * Test strcmp first because we use it to test other things.
  92.      */
  93.     it = "strcmp";
  94.     check(strcmp("", "") == 0, 1);        /* Trivial case. */
  95.     check(strcmp("a", "a") == 0, 2);    /* Identity. */
  96.     check(strcmp("abc", "abc") == 0, 3);    /* Multicharacter. */
  97.     check(strcmp("abc", "abcd") < 0, 4);    /* Length mismatches. */
  98.     check(strcmp("abcd", "abc") > 0, 5);
  99.     check(strcmp("abcd", "abce") < 0, 6);    /* Honest miscompares. */
  100.     check(strcmp("abce", "abcd") > 0, 7);
  101.     check(strcmp("a\203", "a") > 0, 8);    /* Tricky if char signed. */
  102.     if (charsigned)                /* Sign-bit comparison. */
  103.         check(strcmp("a\203", "a\003") < 0, 9);
  104.     else
  105.         check(strcmp("a\203", "a\003") > 0, 9);
  106.  
  107.     /*
  108.      * Test strcpy next because we need it to set up other tests.
  109.      */
  110.     it = "strcpy";
  111.     check(strcpy(one, "abcd") == one, 1);    /* Returned value. */
  112.     equal(one, "abcd", 2);            /* Basic test. */
  113.  
  114.     (void) strcpy(one, "x");
  115.     equal(one, "x", 3);            /* Writeover. */
  116.     equal(one+2, "cd", 4);            /* Wrote too much? */
  117.  
  118.     (void) strcpy(two, "hi there");
  119.     (void) strcpy(one, two);
  120.     equal(one, "hi there", 5);        /* Basic test encore. */
  121.     equal(two, "hi there", 6);        /* Stomped on source? */
  122.  
  123.     (void) strcpy(one, "");
  124.     equal(one, "", 7);            /* Boundary condition. */
  125.  
  126.     /*
  127.      * strcat
  128.      */
  129.     it = "strcat";
  130.     (void) strcpy(one, "ijk");
  131.     check(strcat(one, "lmn") == one, 1);    /* Returned value. */
  132.     equal(one, "ijklmn", 2);        /* Basic test. */
  133.  
  134.     (void) strcpy(one, "x");
  135.     (void) strcat(one, "yz");
  136.     equal(one, "xyz", 3);            /* Writeover. */
  137.     equal(one+4, "mn", 4);            /* Wrote too much? */
  138.  
  139.     (void) strcpy(one, "gh");
  140.     (void) strcpy(two, "ef");
  141.     (void) strcat(one, two);
  142.     equal(one, "ghef", 5);            /* Basic test encore. */
  143.     equal(two, "ef", 6);            /* Stomped on source? */
  144.  
  145.     (void) strcpy(one, "");
  146.     (void) strcat(one, "");
  147.     equal(one, "", 7);            /* Boundary conditions. */
  148.     (void) strcpy(one, "ab");
  149.     (void) strcat(one, "");
  150.     equal(one, "ab", 8);
  151.     (void) strcpy(one, "");
  152.     (void) strcat(one, "cd");
  153.     equal(one, "cd", 9);
  154.  
  155.     /*
  156.      * strncat - first test it as strcat, with big counts, then
  157.      * test the count mechanism.
  158.      */
  159.     it = "strncat";
  160.     (void) strcpy(one, "ijk");
  161.     check(strncat(one, "lmn", 99) == one, 1);    /* Returned value. */
  162.     equal(one, "ijklmn", 2);        /* Basic test. */
  163.  
  164.     (void) strcpy(one, "x");
  165.     (void) strncat(one, "yz", 99);
  166.     equal(one, "xyz", 3);            /* Writeover. */
  167.     equal(one+4, "mn", 4);            /* Wrote too much? */
  168.  
  169.     (void) strcpy(one, "gh");
  170.     (void) strcpy(two, "ef");
  171.     (void) strncat(one, two, 99);
  172.     equal(one, "ghef", 5);            /* Basic test encore. */
  173.     equal(two, "ef", 6);            /* Stomped on source? */
  174.  
  175.     (void) strcpy(one, "");
  176.     (void) strncat(one, "", 99);
  177.     equal(one, "", 7);            /* Boundary conditions. */
  178.     (void) strcpy(one, "ab");
  179.     (void) strncat(one, "", 99);
  180.     equal(one, "ab", 8);
  181.     (void) strcpy(one, "");
  182.     (void) strncat(one, "cd", 99);
  183.     equal(one, "cd", 9);
  184.  
  185.     (void) strcpy(one, "ab");
  186.     (void) strncat(one, "cdef", 2);
  187.     equal(one, "abcd", 10);            /* Count-limited. */
  188.  
  189.     (void) strncat(one, "gh", 0);
  190.     equal(one, "abcd", 11);            /* Zero count. */
  191.  
  192.     (void) strncat(one, "gh", 2);
  193.     equal(one, "abcdgh", 12);        /* Count and length equal. */
  194.  
  195.     /*
  196.      * strncmp - first test as strcmp with big counts, then test
  197.      * count code.
  198.      */
  199.     it = "strncmp";
  200.     check(strncmp("", "", 99) == 0, 1);    /* Trivial case. */
  201.     check(strncmp("a", "a", 99) == 0, 2);    /* Identity. */
  202.     check(strncmp("abc", "abc", 99) == 0, 3);    /* Multicharacter. */
  203.     check(strncmp("abc", "abcd", 99) < 0, 4);    /* Length unequal. */
  204.     check(strncmp("abcd", "abc", 99) > 0, 5);
  205.     check(strncmp("abcd", "abce", 99) < 0, 6);    /* Honestly unequal. */
  206.     check(strncmp("abce", "abcd", 99) > 0, 7);
  207.     check(strncmp("a\203", "a", 2) > 0, 8);    /* Tricky if '\203' < 0 */
  208.     if (charsigned)                /* Sign-bit comparison. */
  209.         check(strncmp("a\203", "a\003", 2) < 0, 9);
  210.     else
  211.         check(strncmp("a\203", "a\003", 2) > 0, 9);
  212.     check(strncmp("abce", "abcd", 3) == 0, 10);    /* Count limited. */
  213.     check(strncmp("abce", "abc", 3) == 0, 11);    /* Count == length. */
  214.     check(strncmp("abcd", "abce", 4) < 0, 12);    /* Nudging limit. */
  215.     check(strncmp("abc", "def", 0) == 0, 13);    /* Zero count. */
  216.  
  217.     /*
  218.      * strncpy - testing is a bit different because of odd semantics
  219.      */
  220.     it = "strncpy";
  221.     check(strncpy(one, "abc", 4) == one, 1);    /* Returned value. */
  222.     equal(one, "abc", 2);            /* Did the copy go right? */
  223.  
  224.     (void) strcpy(one, "abcdefgh");
  225.     (void) strncpy(one, "xyz", 2);
  226.     equal(one, "xycdefgh", 3);        /* Copy cut by count. */
  227.  
  228.     (void) strcpy(one, "abcdefgh");
  229.     (void) strncpy(one, "xyz", 3);        /* Copy cut just before NUL. */
  230.     equal(one, "xyzdefgh", 4);
  231.  
  232.     (void) strcpy(one, "abcdefgh");
  233.     (void) strncpy(one, "xyz", 4);        /* Copy just includes NUL. */
  234.     equal(one, "xyz", 5);
  235.     equal(one+4, "efgh", 6);        /* Wrote too much? */
  236.  
  237.     (void) strcpy(one, "abcdefgh");
  238.     (void) strncpy(one, "xyz", 5);        /* Copy includes padding. */
  239.     equal(one, "xyz", 7);
  240.     equal(one+4, "", 8);
  241.     equal(one+5, "fgh", 9);
  242.  
  243.     (void) strcpy(one, "abc");
  244.     (void) strncpy(one, "xyz", 0);        /* Zero-length copy. */
  245.     equal(one, "abc", 10);    
  246.  
  247.     (void) strncpy(one, "", 2);        /* Zero-length source. */
  248.     equal(one, "", 11);
  249.     equal(one+1, "", 12);    
  250.     equal(one+2, "c", 13);
  251.  
  252.     (void) strcpy(one, "hi there");
  253.     (void) strncpy(two, one, 9);
  254.     equal(two, "hi there", 14);        /* Just paranoia. */
  255.     equal(one, "hi there", 15);        /* Stomped on source? */
  256.  
  257.     /*
  258.      * strlen
  259.      */
  260.     it = "strlen";
  261.     check(strlen("") == 0, 1);        /* Empty. */
  262.     check(strlen("a") == 1, 2);        /* Single char. */
  263.     check(strlen("abcd") == 4, 3);        /* Multiple chars. */
  264.  
  265.     /*
  266.      * strchr
  267.      */
  268.     it = "strchr";
  269.     check(strchr("abcd", 'z') == NULL, 1);    /* Not found. */
  270.     (void) strcpy(one, "abcd");
  271.     check(strchr(one, 'c') == one+2, 2);    /* Basic test. */
  272.     check(strchr(one, 'd') == one+3, 3);    /* End of string. */
  273.     check(strchr(one, 'a') == one, 4);    /* Beginning. */
  274.     check(strchr(one, '\0') == one+4, 5);    /* Finding NUL. */
  275.     (void) strcpy(one, "ababa");
  276.     check(strchr(one, 'b') == one+1, 6);    /* Finding first. */
  277.     (void) strcpy(one, "");
  278.     check(strchr(one, 'b') == NULL, 7);    /* Empty string. */
  279.     check(strchr(one, '\0') == one, 8);    /* NUL in empty string. */
  280.  
  281.     /*
  282.      * index - just like strchr
  283.      */
  284.     it = "index";
  285.     check(index("abcd", 'z') == NULL, 1);    /* Not found. */
  286.     (void) strcpy(one, "abcd");
  287.     check(index(one, 'c') == one+2, 2);    /* Basic test. */
  288.     check(index(one, 'd') == one+3, 3);    /* End of string. */
  289.     check(index(one, 'a') == one, 4);    /* Beginning. */
  290.     check(index(one, '\0') == one+4, 5);    /* Finding NUL. */
  291.     (void) strcpy(one, "ababa");
  292.     check(index(one, 'b') == one+1, 6);    /* Finding first. */
  293.     (void) strcpy(one, "");
  294.     check(index(one, 'b') == NULL, 7);    /* E